package com.systop.servlet;

import com.systop.entity.Notice;
import com.systop.service.NoticeService;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NoticeAddServlet extends HttpServlet {

    private NoticeService noticeService = new NoticeService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        /*乱码的设置,设置到tomcat 的编码的格式 本身编码 */
        response.setCharacterEncoding("UTF-8");
        /*页面的编码的格式*/
        response.setContentType("text/html;charset=UTF-8");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String pushTime = request.getParameter("pushTime");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");

        try {
            Date date = format.parse(pushTime);
            Notice notice = new Notice();
            notice.setTitle(title);
            notice.setContent(content);
            notice.setPushTime(date);
            noticeService.add(notice);
            response.sendRedirect("success.jsp");
        } catch (ParseException e) {
            response.sendRedirect("error.jsp");
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }
}
